package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZdeliveryMapper;
import cn.gson.fengboot.model.mapper.ZdeliverylogMapper;
import cn.gson.fengboot.model.mapper.ZorderMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZdeliveryService {
    @Autowired
    ZdeliveryMapper fpmapper;

    @Autowired
    ZinvoiceDao fpdao;

    @Autowired
    ZdeliveryDao dao;

    @Autowired
    ZdeliverydeliveryDao xqdao;

    @Autowired
    ZorderDao dddao;

    @Autowired
    ZorderdetailsDao ddxqdao;

    @Autowired
    ZorderMapper ddmapper;

    @Autowired
    ZbarterDao hhdao;
    /*
        交付计划弹框
     */
    public List<Zdelivery> kaipiao(String search,String search2,String search3){
        return fpmapper.kaipiao(search,search2,search3);
    }

    /*
        交付计划列表查询
     */
    public List<Zdelivery> jihua(String search,String search2){
        return fpmapper.jihua(search,search2);
    }

    /*
        交付计划详情
     */
    public List<Zdeliverydelivery> kaipiao2(Integer search){
        return fpmapper.kaipiao2(search);
    }

    /*
        交付计划订单详情页
     */
    public List<Zdelivery> juaofujihuadd(String search){
        return fpmapper.juaofujihuadd(search);
    }

    /*
    交付计划新增修改
    */
    public void add(Zdelivery jh){ //新增订单表
        Zdelivery save = dao.save(jh);
        //新增采购详情表
        jh.getXq().forEach(pur->{
            pur.setDeliveryId(save);
        });
        xqdao.saveAll(jh.getXq());

        //修改订单详情表的数据
        List<Zorderdetails> allpro = ddmapper.allpro(jh.getOrderId().getOrderId());
        for (int i =0;i<allpro.size();i++) {
            for (int j =0;j<jh.getXq().size();j++) {
                if(allpro.get(i).getProdetailId().getProdetailId()==jh.getXq().get(j).getProdetailId().getProdetailId()){
                    allpro.get(i).setOrderdetailJfnumber(allpro.get(i).getOrderdetailJfnumber()-jh.getXq().get(j).getDeliverydeliveryNumber());
                }
            }
        }
        ddxqdao.saveAll(allpro);

        Integer sfth = 0;
            for (int i =0;i<allpro.size();i++) {
                if(allpro.get(i).getOrderdetailJfnumber()!=0){
                    sfth=1;
                }
            }

        Zorder zorder = dddao.findById(jh.getOrderId().getOrderId()).get();
        if(zorder.getBarterId().getBarterId()!=-1){
            Zbarter zbarter = hhdao.findById(zorder.getBarterId().getBarterId()).get();
            zbarter.setBarterState("发货中");
            hhdao.save(zbarter);
        }
        if (sfth==0){
            zorder.setOrderJfzt("已交付");
        }else {
            zorder.setOrderJfzt("交付中");
        }

        dddao.save(zorder);


    }
    public void sc(Zdelivery jh){
        //修改订单详情表的数据
        List<Zorderdetails> allpro = ddmapper.allpro(jh.getOrderId().getOrderId());
        List<Zdeliverydelivery> zdeliverydeliveries = fpmapper.kaipiao2(jh.getDeliveryId());
        for (int i =0;i<allpro.size();i++) {
            for (int j =0;j<zdeliverydeliveries.size();j++) {
                if(allpro.get(i).getProdetailId().getProdetailId()==zdeliverydeliveries.get(j).getProdetailId().getProdetailId()){
                    allpro.get(i).setOrderdetailJfnumber(allpro.get(i).getOrderdetailJfnumber()+zdeliverydeliveries.get(j).getDeliverydeliveryNumber());
                }
            }
        }
        ddxqdao.saveAll(allpro);
        dao.delete(jh);
    }

    //<!--谭方圆=>客户合并的交付计划查询-->
    public List<Zdelivery> tfyjfjh(Integer id1, Integer id2){
        return fpmapper.tfyjfjh(id1,id2);
    }
}
